const conn = require('../../libs/DB/Conn');
const operationRecord = require("../sys/operationRecordAction");

module.exports = async function (params) {
    let checkUserSql = `SELECT
                            count(*) AS count
                        FROM
                            user_file uf
                        WHERE user_id != ?
                            AND login_name = ? 
                            AND delete_sign = 1
                        ;
                        SELECT
                            count(*) AS count
                        FROM
                            user_group ug
                        INNER JOIN user_file uf ON uf.domain_id = ug.domain_id
                        AND user_id = ?
                        WHERE
                            ug.group_id = ?
                            AND ug.delete_sign = 1`;

    let queryResult = await conn(checkUserSql, [params.userId, params.loginName]);
    if (queryResult[0][0].count > 0) {
        return {success: false, message: '此登录名已存在，请重新输入登录名'};
    }else if(queryResult[1][0].count === 0){
        return {success: true, message: '当前管理的区域下没有此用户组'};
    } else {
        let updateObj = {
          login_name:params.loginName,
          user_name:params.userName,
          group_id:params.groupId,
          mobile:params.mobile
        };
        let updateSql = `update user_file SET ? WHERE user_id = ?`;
        await conn(updateSql, [updateObj, params.userId]);
        await operationRecord(params);
        return {success: true, message: '修改成功'}
    }
};